﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ComputerizedRehabForms
{
    public partial class BellsTestForm : Form
    {
        Random rnd = new Random();
        bool hasSelected = false;
        List<string> imageNames = new List<string>();
        List<string> selectedBefore = new List<string>();
        Random r = new Random();
        int numberOfSelections = 0;
        //WARNING - This cannot be too high, else we're going to hit an infinite loop
        int selectionThreshold = 3;
        BellsButton LButton = new BellsButton();
        BellsButton MButton = new BellsButton();
        BellsButton RButton = new BellsButton();
        int bellImageTurn = 0;
        bool shownBell = false;

        public BellsTestForm()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
            InitializeComponent();
            imageNames.Add("Apple");
            imageNames.Add("Bell");
            imageNames.Add("Bird");
            imageNames.Add("Car");
            imageNames.Add("Cloud");
            imageNames.Add("Fish");
            imageNames.Add("Guitar");
            imageNames.Add("Gun");
            imageNames.Add("Handsaw");
            imageNames.Add("Horse");
            imageNames.Add("House");
            imageNames.Add("Key");
            imageNames.Add("Mallet");
            imageNames.Add("Teapot");
            imageNames.Add("Tree");
            bellImageTurn = r.Next(selectionThreshold); //Determine when Bell is shown
            selectedBefore.Add("Bell"); //Bell can't be a random image



            GenerateSelection();
        }

        private void BellsTestForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void GenerateSelection()
        {
            if (numberOfSelections == selectionThreshold + 1)
            {
                numberOfSelections = 0;
                DataManager.hideBellsIntro();
                DataManager.showBellsTest();

            }
            else if (numberOfSelections == bellImageTurn)
            {
                showBell();
                shownBell = true;
            }
            else
            {
                //Continue with the selection intro

                //Clear out the button text
                LButton.Text = String.Empty;
                MButton.Text = String.Empty;
                RButton.Text = String.Empty;

                // Randomize button selection. No duplicates!
                List<string> buttonNames = new List<string>();

                //Assign Left button
                int index = r.Next(imageNames.Count);
                string randomString = imageNames[index];
                imageNames.RemoveAt(index);
                while (selectedBefore.Contains(randomString))
                {
                    index = r.Next(imageNames.Count);
                    randomString = imageNames[index];
                }
                buttonNames.Add(randomString);
                LButton.Text = randomString;

                //Assign Middle Button
                index = r.Next(imageNames.Count);
                randomString = imageNames[index];
                imageNames.RemoveAt(index);
                while (selectedBefore.Contains(randomString))
                {
                    index = r.Next(imageNames.Count);
                    randomString = imageNames[index];
                }
                buttonNames.Add(randomString);
                MButton.Text = randomString;

                //Assign Right Button
                index = r.Next(imageNames.Count);
                randomString = imageNames[index];
                imageNames.RemoveAt(index);
                while (selectedBefore.Contains(randomString))
                {
                    index = r.Next(imageNames.Count);
                    randomString = imageNames[index];
                }
                buttonNames.Add(randomString);
                RButton.Text = randomString;

                //Make image from one of the button names
                index = r.Next(buttonNames.Count);
                randomString = buttonNames[index];

                //Check to see if the image was selected before.
                //Loop until you find a new image
                while (selectedBefore.Contains(randomString))
                {
                    index = r.Next(buttonNames.Count);
                    randomString = buttonNames[index];
                }
                //You have selected an image.
                //Add it to the selected list
                selectedBefore.Add(randomString);

                Bitmap b = new Bitmap("../Bells_Images/" + randomString + ".jpg");
                b.MakeTransparent(Color.White);
                pictureBox1.Image = b;

                //Add back in to the image list
                imageNames.AddRange(buttonNames);

                //clear out button names
                buttonNames.Clear();

                //increment numberOfSelections
                numberOfSelections++;
            }
        }

        private void showBell()
        {
            //Clear out the button text
            LButton.Text = String.Empty;
            MButton.Text = String.Empty;
            RButton.Text = String.Empty;

            // Randomize button selection. No duplicates!
            List<string> buttonNames = new List<string>();

            //Assign Left button
            int index = r.Next(imageNames.Count);
            string randomString = imageNames[index];
            imageNames.RemoveAt(index);
            while (selectedBefore.Contains(randomString))
            {
                index = r.Next(imageNames.Count);
                randomString = imageNames[index];
            }
            buttonNames.Add(randomString);
            LButton.Text = randomString;

            //Assign Middle Button
            index = r.Next(imageNames.Count);
            randomString = imageNames[index];
            imageNames.RemoveAt(index);
            while (selectedBefore.Contains(randomString))
            {
                index = r.Next(imageNames.Count);
                randomString = imageNames[index];
            }
            buttonNames.Add(randomString);
            MButton.Text = randomString;

            //Assign Right Button
            index = r.Next(imageNames.Count);
            randomString = imageNames[index];
            imageNames.RemoveAt(index);
            while (selectedBefore.Contains(randomString))
            {
                index = r.Next(imageNames.Count);
                randomString = imageNames[index];
            }
            buttonNames.Add(randomString);
            RButton.Text = randomString;

            //All of the buttons have random names now, pick one to be the bell
            index = r.Next(1, 3);
            if (index == 1) //Left Button
            {
                LButton.Text = "Bell";
            }
            else if (index == 2) //Middle Button
            {
                MButton.Text = "Bell";
            }
            else if (index == 3) //Right Button
            {
                RButton.Text = "Bell";
            }

            //Change picture to Bell
            Bitmap b = new Bitmap("../Bells_Images/Bell.jpg");
            b.MakeTransparent(Color.White);
            pictureBox1.Image = b;

            //Add back in to the image list
            imageNames.AddRange(buttonNames);

            //clear out button names
            buttonNames.Clear();

            //increment numberOfSelections
            numberOfSelections++;
        }

        private void lbutton_Click(object sender, EventArgs e)
        {
            //the other buttons have to be set the the default color.
            makeButtonsDefaultColor();

            //Highlight the button, set selection to true
            LButton.BackColor = Color.Yellow;
            hasSelected = true;
        }

        private void mbutton_Click(object sender, EventArgs e)
        {
            //the other buttons have to be set the the default color.
            makeButtonsDefaultColor();

            //Highlight the button, set selection to true
            MButton.BackColor = Color.Yellow;
            hasSelected = true;

        }

        private void rbutton_Click(object sender, EventArgs e)
        {
            //the other buttons have to be set the the default color.
            makeButtonsDefaultColor();

            //Highlight the button, set selection to true
            RButton.BackColor = Color.Yellow;
            hasSelected = true;
        }

        private void BellsTestForm_KeyDown(object sender, KeyEventArgs e)
        {
            //if user has made a selection
            if (hasSelected)
            {
                //If the enter key is pressed, to confirm correct selection
                if (e.KeyCode == Keys.Enter)
                {
                    makeButtonsDefaultColor();

                    //Generate a new selection, set hasSelected to false for the new selection
                    GenerateSelection();
                    hasSelected = false;
                }
            }
        }

        /*
         * This private method will set all the buttons to the default color.
         * */
        private void makeButtonsDefaultColor()
        {
            LButton.BackColor = SystemColors.Control;
            LButton.UseVisualStyleBackColor = true;
            MButton.BackColor = SystemColors.Control;
            MButton.UseVisualStyleBackColor = true;
            RButton.BackColor = SystemColors.Control;
            RButton.UseVisualStyleBackColor = true;
        }
        /**
         * This method creates the buttons that will be clicked.
         * They are custom buttons in order to get rid of the property of
         * 'when clicking box, hitting enter "clicks" the box'         * 
         * */
        private void BellsTestForm_Load(object sender, EventArgs e)
        {
            //Create Left button!
            LButton.Show();
            LButton.BringToFront();
            LButton.Height = left_button.Size.Height;
            LButton.Width = left_button.Size.Width;
            LButton.Click += new EventHandler(lbutton_Click);
            this.Controls.Add(LButton);
            left_button.Hide();
            left_button.SendToBack();

            //Create middle button!
            MButton.Show();
            MButton.BringToFront();
            MButton.Height = middle_button.Size.Height;
            MButton.Width = middle_button.Size.Width;
            MButton.Click += new EventHandler(mbutton_Click);
            this.Controls.Add(MButton);
            middle_button.Hide();
            middle_button.SendToBack();

            //Create right button!
            RButton.Show();
            RButton.BringToFront();
            RButton.Height = right_button.Size.Height;
            RButton.Width = right_button.Size.Width;
            RButton.Click += new EventHandler(rbutton_Click);
            this.Controls.Add(RButton);
            right_button.Hide();
            right_button.SendToBack();

            //Add the new buttons to the table layout
            tableLayoutPanel1.Controls.Add(LButton, 0, 1);
            tableLayoutPanel1.Controls.Add(MButton, 1, 1);
            tableLayoutPanel1.Controls.Add(RButton, 2, 1);
        }

    }
}

public class BellsButton : Button
{
    public BellsButton()
    {
        this.SetStyle(ControlStyles.Selectable, false);
        this.Font = new Font(this.Font.FontFamily, 24);
    }
}